#!/bin/bash
# Locate file with specific text inside a file using "find".
# 
# usage: findtext where what
# example: findtext ~/workspace/codebase/ Callback
# example: findtext ~/workspace/codebase/ "class Square"
#
# It also supports regular expression (the format is the same as in egrep):
# example: findtext ~/workspace/codebase/ "class .*Square"
# example: findtext ~/workspace/codebase/ "Square|Rectangle"

if [ $# -ne 2 ]
then
  echo "Error: incorrect number of arguments"
  echo "usage: findtext path pattern"
  exit $E_BADARGS
fi

if [ -z "$1" ]
then
  echo "Error: empty path"
  echo "usage: findtext path pattern"
  exit $E_BADARGS
fi

if [ -z "$2" ]
then
  echo "Error: empty pattern"
  echo "usage: findtext path pattern"
  exit $E_BADARGS
fi

find "$1" -path '*/.svn' -prune -o -path './third_party/*' -prune -o -path './3rdparty/*' -prune -o -type f -exec egrep --color=auto --binary-files=without-match -E "$2" {} /dev/null \;
# comment: egrep is case-sensitive by default; use -i to ignore case.
